import { Layout } from "@/components/layout/layout";
import Redirect from "@/components/redirect";
import { getAuthInfo } from "../utils/front";
import type { NextPage, GetServerSidePropsContext } from "next";
import Link from "next/link";
import Router, { useRouter } from "next/router";

const withAuth = (Page: NextPage) => {
  const WrapperPage: NextPage<{ token: string }> = ({ token }) => {
    // console.log("🚀 ~ withAuth ~ token:", token);
    if (!token) {
      return <Redirect path="/login" tips="Please log in first" />;
    }

    return <Page />;
  };

  return WrapperPage;
};

export default withAuth;

export const getServerSideProps = async (
  context: GetServerSidePropsContext
) => {
  const { token } = getAuthInfo(context);
  return {
    props: {
      token,
    },
  };
};
